import { GoodsITF } from '@INTERFACES/Goods'
import { LanguageType } from '@INTERFACES/Common'
import React from 'react'
import Taro from '@tarojs/taro'
import { Analytic_Type } from '@CONSTANTS/analytic'
import { Image, Text, View } from '@tarojs/components'
import DBConfig from '@CONFIG/DBConfig'
import { getPlatform } from '@UTILS/common'
import style from './GoodsItem.module.scss'

const { isWX, isWeb } = getPlatform()

interface GoodsItemProps {
  data: GoodsITF;
  language: LanguageType;
  containerClass?: string;
  eventId?: string;
  eventParams?: unknown;
  itemClass?: string;
}

const GoodsItem: React.FC<GoodsItemProps> = ({
  data, language, containerClass, eventId, eventParams, itemClass
}) => {
  const { Description, Name, OriginalPrice, Price, PreviewFileName, Url } = data
  const onClick = () => {
    Taro.tracker.trackEvent(eventId || Analytic_Type.tap_goods_item.eventId, eventParams || { goodsName: Name?.[language] })
    if (isWeb) {
      // eslint-disable-next-line no-restricted-globals
      top.location.href = Url.H5Path
    } else if (isWX) {
      Taro.navigateToMiniProgram({
        appId: Url?.wxAppId,
        path: Url?.WmpPath
      })
    }
  }
  return (
    <View className={`${style.goodsItem} ${containerClass}`} onClick={onClick}>
      <Image
        src={`${DBConfig.staticGoodsUrl}${PreviewFileName}?x-oss-process=image/resize,w_160`}
        className={style.goodsImage}
      />
      <View className={`${style.content} ${itemClass}`}>
        <View className={style.nameBox}><Text className={style.goodsName}>{Name?.[language]}</Text></View>
        <View className={style.descBox}><Text className={style.goodsDesc}>{Description?.[language]}</Text></View>
        <View className={style.goodsPriceRow}>
          <Text className={style.goodsPrice}>
            <Text className={style.goodsPriceSmall}>¥</Text>
            {(Price || 0).toFixed(2)}
          </Text>
          <Text className={style.originalPrice}>{(OriginalPrice || 0).toFixed(2)}</Text>
        </View>
      </View>
    </View>
  )
}

export default GoodsItem
